package net.xju.controller;

import net.xju.common.framework.simplemvc.RequestMapping;
import net.xju.common.utils.JSONUtil;
import net.xju.common.utils.PageInfo;
import net.xju.common.utils.Result;
import net.xju.entity.Announcement;
import net.xju.service.AnnouncementService;
import net.xju.service.impl.AnnouncementServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;

@RequestMapping("/api/announcement")
public class AnnouncementController {
    AnnouncementService announcementService = new AnnouncementServiceImpl();

/*
    @RequestMapping("/list.do")  // 修改方法映射
    public void list(HttpServletRequest req, HttpServletResponse resp) {
        String title = req.getParameter("title");
        String pageNoStr = req.getParameter("pageNo");
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageNo = pageNoStr == null || pageNoStr.isEmpty() ? 1 : Integer.parseInt(pageNoStr);
        Integer pageSize = pageSizeStr == null || pageSizeStr.isEmpty() ? 10 : Integer.parseInt(pageSizeStr);
        PageInfo<Announcement> pageInfo = announcementService.pageList(title, pageNo, pageSize);
        JSONUtil.printByJSON(resp, Result.success(pageInfo));
    }
*/


    @RequestMapping("/pageList.do")
    public void pageList(HttpServletRequest req, HttpServletResponse resp) {
        String title = req.getParameter("title");
        String pageNoStr = req.getParameter("pageNo");
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageNo = pageNoStr == null || pageNoStr.isEmpty() ? 1 : Integer.parseInt(pageNoStr);
        Integer pageSize = pageSizeStr == null || pageSizeStr.isEmpty() ? 10 : Integer.parseInt(pageSizeStr);
        PageInfo<Announcement> pageInfo = announcementService.pageList(title, pageNo, pageSize);
        JSONUtil.printByJSON(resp, Result.success(pageInfo));
    }

    @RequestMapping("/insert.do")
    public void insert(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Announcement announcement = new Announcement();
        announcement.setTitle(req.getParameter("title"));
        announcement.setContent(req.getParameter("content"));
        announcement.setCreateTime(LocalDateTime.now());
        announcement.setUpdateTime(LocalDateTime.now());
        announcementService.insert(announcement);
        JSONUtil.printByJSON(resp, Result.success("添加成功"));
    }

    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Announcement announcement = new Announcement();
        announcement.setId(Integer.parseInt(req.getParameter("id")));
        announcement.setTitle(req.getParameter("title"));
        announcement.setContent(req.getParameter("content"));
        announcement.setUpdateTime(LocalDateTime.now());
        if (announcementService.update(announcement)) {
            JSONUtil.printByJSON(resp, Result.success("更新成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("更新失败"));
        }
    }

    @RequestMapping("/selectById.do")
    public void selectById(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        Announcement announcement = announcementService.selectById(Integer.parseInt(id));
        JSONUtil.printByJSON(resp, Result.success(announcement));
    }

    @RequestMapping("/deleteById.do")
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        if (announcementService.deleteById(Integer.parseInt(id))) {
            JSONUtil.printByJSON(resp, Result.success("删除成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.success("删除失败"));
        }
    }
}